package com.lcz.cloud_note.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.lcz.cloud_note.dao.ActionDetailDao;
import com.lcz.cloud_note.dao.LikeDao;
import com.lcz.cloud_note.entity.ActionDetail;
import com.lcz.cloud_note.entity.Like;
import com.lcz.cloud_note.service.ShareService;
import com.lcz.cloud_note.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lcz.cloud_note.dao.NoteDao;
import com.lcz.cloud_note.dao.ShareDao;
import com.lcz.cloud_note.entity.Note;
import com.lcz.cloud_note.entity.Share;
import com.lcz.cloud_note.util.NoteResult;
import com.lcz.cloud_note.util.NoteUtil;
@Service("shareService")//增加到spring容器里面
@Transactional //spring事务
public class ShareServiceImpl  implements ShareService {
	@Resource
	private ShareDao shareDao;
	@Resource
	private NoteDao noteDao;
	@Autowired
	private LikeDao likeDao;
	@Autowired
	private ActionDetailDao actionDetailDao;
	//分享功能(实际上为增加内容)
	public Result shareNote(String noteId)
	{

		//先查询是否被分享过
		Share share = shareDao.findByNoteId(noteId);
		//获取笔记标题和内容
		Note note=noteDao.findByNoteId(noteId);
		//向cn_share表中插入记录
		if(share!=null)
		{
			//查询是否内容或标题被改变
			if(share.getCn_share_title().equalsIgnoreCase(note.getCn_note_title())
					&& share.getCn_share_title().equalsIgnoreCase(note.getCn_note_body()))
			{
				return Result.fail("该笔记已被分享过");
			}else
				{
					share.setCn_share_title(note.getCn_note_title());
					share.setCn_share_body(note.getCn_note_body());
					shareDao.dynamicUpdate(share);
					return Result.ok("笔记分享已更新");
				}
		}
		share = new Share();
		String shareId = NoteUtil.createId();
		share.setCn_share_id(shareId);//主键
		share.setCn_note_id(noteId);

		share.setCn_share_title(note.getCn_note_title());
		share.setCn_share_body(note.getCn_note_body());
		//保存分享记录
		shareDao.share(share);
		return Result.ok("笔记分享成功");
	}
	//搜索功能
	public NoteResult<List<Share>> searchNote(String keyword, int page) {
		String title="%"+keyword+"%";
		//模糊查询
        int begin = (page-1) * 15;
        int end = begin+14;
		List<Share> shares = shareDao.findLikeTitle(title,begin,end);
		//构建返回结果
		NoteResult<List<Share>> result = new NoteResult<List<Share>>();
		result.setStatus(0);
		result.setMsg("搜索成功");
		result.setData(shares);
		return result;
	}
	//点击搜索后的收藏笔记，从而查看笔记信息
	public NoteResult<Share> loadShareNote(String shareId) {
		Share share = shareDao.findById(shareId);
		NoteResult result = new NoteResult();
		result.setStatus(0);
		result.setMsg("加载笔记成功");
		result.setData(share);
		return result;
	}

	public Result collect(String userId, String shareId)
	{
		//查询现在分享的笔记
		Share share = shareDao.findById(shareId);
		//查询用户是否已收藏过该分享的笔记
		Like like = likeDao.findByUserIdAndNoteId(userId,share.getCn_note_id());
		if(like == null)
		{//创建收藏笔记本
			like = new Like();
			like.setCn_like_id(NoteUtil.createId());
			like.setCn_like_title(share.getCn_share_title());
			like.setCn_like_body(share.getCn_share_body());
			like.setCn_note_id(share.getCn_note_id());
			like.setCn_user_id(userId);
			likeDao.insert(like);
			return  Result.ok("笔记收藏成功");
		}else
			{//如果内容没改变提示
				if(like.getCn_like_title().equalsIgnoreCase(share.getCn_share_title())
				&& like.getCn_like_body().equalsIgnoreCase(share.getCn_share_body())
				)
				{
					return Result.fail("该笔记已收藏过");
				}else
					{//如果标题或者内容改变,则更新收藏
						like.setCn_like_title(share.getCn_share_title());
						like.setCn_like_body(share.getCn_share_body());
						likeDao.dynamicUpdate(like);
						return Result.ok("更新收藏成功");
					}
			}
	}

	public Result loanCollect(String userId)
	{
		List<Like> likes = likeDao.listByUserId(userId);
		return Result.ok(likes);
	}

	public Result removeCollect(String likeId)
	{
		likeDao.deleteById(likeId);
		return Result.ok();
	}

	public Result showCollect(String likeId)
	{
		return Result.ok(likeDao.findById(likeId));
	}

	public Result collectForAction(String userId, String actionDetailId)
	{
		//查询现在分享的笔记
		ActionDetail actionDetail = actionDetailDao.getById(actionDetailId);
		//查询用户是否已收藏过该分享的笔记
		Like like = likeDao.findByUserIdAndNoteId(userId,actionDetail.getCn_note_id());
		if(like == null)
		{//创建收藏笔记本
			like = new Like();
			like.setCn_like_id(NoteUtil.createId());
			like.setCn_like_title(actionDetail.getCn_note_activity_title());
			like.setCn_like_body(actionDetail.getCn_note_activity_body());
			like.setCn_note_id(actionDetail.getCn_note_id());
			like.setCn_user_id(userId);
			likeDao.insert(like);
			return  Result.ok("活动笔记收藏成功");
		}else
		{//如果内容没改变提示
			if(like.getCn_like_title().equalsIgnoreCase(actionDetail.getCn_note_activity_title())
					&& like.getCn_like_body().equalsIgnoreCase(actionDetail.getCn_note_activity_body())
			)
			{
				return Result.fail("该笔记已收藏过");
			}else
			{//如果标题或者内容改变,则更新收藏
				like.setCn_like_title(actionDetail.getCn_note_activity_title());
				like.setCn_like_body(actionDetail.getCn_note_activity_body());
				likeDao.dynamicUpdate(like);
				return Result.ok("更新收藏成功");
			}
		}
	}

}
